package com.youren.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.youren.health.dao.MemberDao;
import com.youren.health.pojo.Member;
import com.youren.health.service.MemberService;
import com.youren.health.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: health_parent
 * @author: You_ren_a
 * @create: 2020-11-30 19:56
 **/
@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        Member member = memberDao.findByPhone(telephone);
        return member;
    }

    @Override
    public void add(Member member) {
        memberDao.add(member);
    }

    @Override
    public Map<String, Object> getMemberByMouth() {
        List<String> mouths = new ArrayList<>();
        List<Integer> memberCount = new ArrayList<>();
        HashMap<String, Object> resultMap = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, +1);
            Date date = calendar.getTime();
            mouths.add(sdf.format(date));
        }
        resultMap.put("months", mouths);
        for (String mouth : mouths) {
            mouth += "-31";
            Integer count = memberDao.getMemberCountByMouth(mouth);
            memberCount.add(count);
        }
        resultMap.put("memberCount", memberCount);
        return resultMap;
    }

    @Override
    public Map<String, Object> getSetmealReport() {
        List<Map<String, Integer>> setmealCount = memberDao.findSetmealCount();
        List<String> setmealName = new ArrayList<>();
        if (setmealCount != null) {
            for (Map<String, Integer> map : setmealCount) {
                setmealName.add(String.valueOf(map.get("name")));
            }
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("setmealCount", setmealCount);
        resultMap.put("setmealName", setmealName);
        return resultMap;
    }

    @Override
    public Map<String, Object> getBusinessReportData() {
        HashMap<String, Object> resultMap = new HashMap<>();
        //reportDate 今天日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String reportDate = sdf.format(new Date());
        //    todayNewMember :0, regdate=reportdate
        Integer todayNewMember = memberDao.findtodayNewMember(reportDate);
        //    totalMember :0,
        Integer totalMember = memberDao.findTotalMember();
        //    thisWeekNewMember :0, regdate>周一
        //获取周一日期
        String thisWeekMonday = sdf.format(DateUtils.getThisWeekMonday());
        Integer thisWeekNewMember = memberDao.findThisWeekNewMember(thisWeekMonday);
        //    thisMonthNewMember :0,
        //获取这月第一天
        String firstDay4ThisMonth = sdf.format(DateUtils.getFirstDay4ThisMonth());
        Integer thisMonthNewMember = memberDao.findThisMonthNewMember(firstDay4ThisMonth);
        //    todayOrderNumber :0, count orderdate=today
        Integer todayOrderNumber = memberDao.fingTodayOrderNumber(reportDate);
        //    todayVisitsNumber :0, orderdate=today orderStatus="已到诊"
        Integer todayVisitsNumber = memberDao.findTodayVisitsNumber(reportDate);
        //    thisWeekOrderNumber :0, orderdate大于周一，小于周日
        //获取周日
        String sundayOfThisWeek = sdf.format(DateUtils.getSundayOfThisWeek());
        Integer thisWeekOrderNumber = memberDao.findThisWeekOrderNumber(thisWeekMonday, sundayOfThisWeek);
        //    thisWeekVisitsNumber :0,orderdate大于周一，小于周日 orderStatus="已到诊"
        Integer thisWeekVisitsNumber = memberDao.findThisWeekVisitsNumber(thisWeekMonday, sundayOfThisWeek);
        //    thisMonthOrderNumber :0,orderdate大于月一，小于月尾
        // 获取月尾
        Date lastDay = DateUtils.getFirstDay4ThisMonth();
        lastDay.setDate(31);
        String lastDay4ThisMonth = sdf.format(lastDay);
        Integer thisMonthOrderNumber = memberDao.findThisMonthOrderNumber(firstDay4ThisMonth, lastDay4ThisMonth);
        //    thisMonthVisitsNumber :0,orderdate大于月一，小于月尾 orderStatus="已到诊"
        Integer thisMonthVisitsNumber = memberDao.findThisMonthVisitsNumber(firstDay4ThisMonth, lastDay4ThisMonth);
        //    hotSetmeal :
        List<Map<String, Object>> hotSetmeal = memberDao.findHotSetmeal();

        resultMap.put("reportDate",reportDate);
        resultMap.put("todayNewMember",todayNewMember);
        resultMap.put("totalMember",totalMember);
        resultMap.put("thisWeekNewMember",thisWeekNewMember);
        resultMap.put("thisMonthNewMember",thisMonthNewMember);
        resultMap.put("todayOrderNumber",todayOrderNumber);
        resultMap.put("todayVisitsNumber",todayVisitsNumber);
        resultMap.put("thisWeekOrderNumber",thisWeekOrderNumber);
        resultMap.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        resultMap.put("thisMonthOrderNumber",thisMonthOrderNumber);
        resultMap.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        resultMap.put("hotSetmeal",hotSetmeal);
        return resultMap;
    }

//    reportDate:null,
//    todayNewMember :0,
//    totalMember :0,
//    thisWeekNewMember :0,
//    thisMonthNewMember :0,
//    todayOrderNumber :0,
//    todayVisitsNumber :0,
//    thisWeekOrderNumber :0,
//    thisWeekVisitsNumber :0,
//    thisMonthOrderNumber :0,
//    thisMonthVisitsNumber :0,
//    hotSetmeal :[
//    {name:'阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐',setmeal_count:200,proportion:0.222},
//    {name:'阳光爸妈升级肿瘤12项筛查体检套餐',setmeal_count:200,proportion:0.222}


}
